package entity;

import java.util.List;

/**
 * Values and methods for specifying the VERSION property
 * for the iCalendar object as defined in RFFC 5545. This 
 * property specifies the identifier corresponding to the
 * highest version number or the minimum and maximum range 
 * of theiCalendar specification that is required in order 
 * to interpret theiCalendar object.
 * 
 * @author Sean Young
 * @version 0.1
 *
 */
public class Version {
	/** The parameters for the VERSION property */
	private String verparam;
	
	/** The value of the VERSION property */
	private String vervalue;
	
	/** The minimum range of the version */
	private String minver;
	
	/** The maximum range of the version */
	private String maxver;
	
	/**
	 * The default constructor for the VERSION property.
	 * All values are defaulted to null.
	 */
	public Version() {
		this.verparam = null;
		this.vervalue = null;
		this.minver = null;
		this.maxver = null;
	}
	
	/**
	 * Constructor for the VERSION property. Sets the value of the 
	 * VERSIONY property to the value specified in the parameter.
	 * @param vervalue The version
	 */
	public Version(String vervalue) {
		this.verparam = null;
		this.vervalue = vervalue;
		this.minver = null;
		this.maxver = null;
	}
	
	/**
	 * Sets the value of the parameter for the VERSION property.
	 *
	 * @param verparam The parameter to be added to the VERSION Property.
	 *                 The parameter must be a single parameter.
	 */
	public void setVerParam(String verparam) {
		this.verparam = verparam;
	}
	
	/**
	 * Sets the value of the parameter for the VERSION property.
	 * @param verparams A list of parameters to be added to the VERSION property.
	 */
	public void setVerParam(List<String> verparams) {
		StringBuilder parameterBuilder = new StringBuilder(" ");
		for(int i = 0; i < verparams.size(); i++) {
			parameterBuilder.append(verparams.get(i));
			if((verparams.size() != 1) && (i != verparams.size() - 1)) parameterBuilder.append(";");
		}
		this.verparam = parameterBuilder.toString();
	}
	
	/**
	 * Sets the value of the VERSION parameter.
	 * @param vervalue The version
	 */
	public void setVerValue(String vervalue) {
		this.vervalue = vervalue;
	}
	
	/**
	 * Sets the maximum value of the VERSION parameter.
	 * @param maxver The maximum version
	 */
	public void setMaxVer(String maxver) {
		this.maxver = maxver;
	}
	
	/**
	 * Sets the minimum value of the VERSION parameter.
	 * @param minver The minimum version
	 */
	public void setMinVer(String minver) {
		this.minver = minver;
	}
	
	/**
	 * Returns a string containing the values of the fields of the
	 * VERSION property. If no parameters were specified then they 
	 * will not be included.
	 */
	public String toString() {
		StringBuilder versionString = new StringBuilder("VERSION");
		if(vervalue == null) return null;
		if(verparam != null) versionString.append(verparam);
		
		versionString.append(":");
		
		if((maxver != null) && (minver == null)) vervalue = maxver;
		if((maxver != null) && (minver != null)) vervalue = minver + ";" + maxver;
		
		versionString.append(vervalue);
		versionString.append("\r\n");
		
		return versionString.toString();
	}
	
}
